<?php
namespace app\applet\model;
use think\Db;
use think\Model;

class OrderHouse extends Model {
    protected  $table = 'order_house';
    public function __construct($data = [])
    {
//        parent::__construct($data);
    }

    /**
     * @param $data
     * @return int|string
     */
    public function m_insert($data) {
        $data = Db::name($this->table)
            ->insert($data);
        return Db::name($this->table)->getLastInsID();
    }

    /**
     * @param $data
     * @return array|false|\PDOStatement|string|Model
     */
    public function keeper_id($data) {
        $data = Db::name('house_keeper')
            ->alias('a')
            ->join('tenant_management b','a.id=b.tenant_user_id','Left')
            ->field('a.name,b.tenant_name,b.tenant_tel')
            ->where(['a.id'=>$data])
            ->find();
        return $data;
    }
    public function user($data) {
        $data = Db::name('admin_user')
            ->where(['admin_id'=>$data])
            ->field('wechat_open_id')
            ->find();
        return $data['wechat_open_id'];
    }
    //预约看房消息推送
    public function Wechat($data){

        // 微信接口类
        $WeChat = new  \Template\Wechat();
        $weiwei_token = $WeChat->getToken(); // 获取微信token
        # 公众号消息推送
        $WeChat::pushMessage([
            'miniprogram'=> [
                'appid' =>'wx9eca16e65fdc2aa4',
                'pagepath' =>'pages/index/buildings/buildings',
            ],
            'openid' => $data['openid'], // 用户openid
            'access_token' => $weiwei_token,
            'template_id' => "SaIgQWbRm45RDxWMIQe3LuKL8-Lo71-WwXxS8as2uJ4", // 填写你自己的消息模板ID
            'data' => [ // 模板消息内容，根据模板详情进行设置
                'first'    => ['value' => urlencode($data['first']),'color' => ""],
                'keyword1' => ['value' => urlencode($data['keyword1']),'color'=>''],
                'keyword2' => ['value' => urlencode($data['keyword2']),'color'=>''],
                'keyword3' => ['value' => urlencode($data['keyword3']),'color' => ''],
                'keyword4' => ['value' => urlencode($data['keyword4']),'color' => ''],
                'remark'   => ['value' => urlencode($data['remark']),'color' => '']
            ],
        ]);
    }
    /**
     * 编辑
     * @param $data
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function m_update($id, $data) {
        $data = Db::name($this->table)
            ->where(['id'=>$id])
            ->update($data);
        return $data;
    }

    /**
     * @param $type
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getCount($type) {
        $data = Db::name($this->table)
            ->field('house_id, count(house_id)')
            ->group('house_id')
            ->having('status=1')
            ->having('type=' . $type)
            ->select();
        return $data;
    }

    /**
     * @param $type
     * @param $id
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOneCount($type, $id) {
        $data = Db::name($this->table)
            ->where('status=1')
            ->where('type=' . $type)
            ->where('house_id=' . $id)
            ->count();
        return $data;
    }

    /**
     * @param $keeper_id
     * @param $page
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function OrderList($keeper_id, $page) {
        $where=[];
        if($keeper_id!==''){
            $where['keeper_id']=$keeper_id;
        }
        $count=Db::name($this->table)
            ->where($where)
            ->where('status','=', '1')
            ->count();
        $data=Db::name($this->table)
            ->where($where)
            ->where('status','=', '1')
            ->page($page, 10)
            ->field('id, keeper_id, house_id, create_time, type, order_time, status, admin_status')
            ->order("type asc, create_time desc")
            ->select();
        $date['data']=$data;
        $date['count']=$count;

        return $date;
    }

    /**
     * @param $keeper_id
     * @param $page
     * @return mixed
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function OrderListAdmin($page, $admin_status, $admin_id) {
        $where=[];
        if ($admin_status !== null) {
            $where['admin_status']=$admin_status;
        }
        if ($admin_status == "") {
            unset($where['admin_status']);
        }
        if (isset($admin_id) && $admin_id !== '') {
            $where['admin_id']=$admin_id;
        }
        $count=Db::name($this->table)
            ->where($where)
            ->count();
        $data=Db::name($this->table)
            ->alias('o')
            ->where($where)
            ->page($page, 10)
            ->field('o.id, o.keeper_id, o.house_id, o.create_time, o.type, o.order_time, o.deal_time, o.status, 
            o.admin_status,h.name,h.icon,h.tel')
            ->join('house_keeper h', 'o.keeper_id=h.id', 'Left')
            ->order(['deal_time' => 'desc', 'create_time' => 'desc'])
            ->select();
        $date['data']=$data;
        $date['count']=$count;

        return $date;
    }

    /**
     * 获取待处理事项
     * @param $admin_id
     * @return int|string
     * @throws \think\Exception
     */
    public function getTodo($admin_id) {
        $where['admin_id']=$admin_id;
        $where['admin_status']=1;

        $count=Db::name($this->table)
            ->where($where)
            ->count();

        return $count;
    }

    /**
     * 唯一性
     * @param $house_id
     * @param $type
     * @param $keeper_id
     * @return int|string
     * @throws \think\Exception
     */
    public function uniqueness($house_id, $type, $keeper_id) {
        $where['status'] = 1;
        $where['house_id'] = $house_id;
        $where['type'] = $type;
        $where['keeper_id'] = $keeper_id;
        $count = Db::name($this->table)
            ->where($where)
            ->count();
        return $count;
    }

    /**
     * @param $id
     * @param $page
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOne1($id, $page) {
        $where['keeper_id'] = $id;
        $data = Db::name($this->table)
            ->where($where)
            ->where('status','=', '1')
            ->field('id, keeper_id, house_id, create_time, type, order_time, status, admin_status')
            ->order("type asc, create_time desc")
            ->page(($page+1), 10)
            ->select();
        return $data;
    }

    /**
     * @param $id
     * @param $page
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getOne2($id, $page) {
        $where['admin_id'] = $id;
        $data = Db::name($this->table)
            ->where($where)
            ->where('o.admin_status','=', '1')
            ->alias('o')
            ->field('o.id, o.keeper_id, o.house_id, o.create_time, o.type, o.order_time, o.deal_time, o.status, 
            o.admin_status,h.name,h.icon,h.tel')
            ->join('house_keeper h', 'o.keeper_id=h.id', 'Left')
            ->order(['deal_time' => 'desc', 'create_time' => 'desc'])
            ->page(($page+1), 10)
            ->select();
        return $data;
    }

}